package org.jeecg.modules.business.third.strategy.sap;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basic.plant.service.IWmsSapWarehouseMapService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.third.entity.WmsOutReceiptMaterialVo;
import org.jeecg.modules.dock.entity.dto.request.ProductReceptionRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.ProductShipmentsRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.ProductReceptionResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "c30SapOutbound")
@Slf4j
@RequiredArgsConstructor
public class C30SapOutbound implements SapOperateStrategy {

    private final IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;
    private final IWmsSapWarehouseMapService wmsSapWarehouseMapService;
    private final SapDockingRepeaterService sapDockingRepeaterService;

    @Override
    public List operate(Map<String, Object> map) {
        log.info("调拨入库(定尺/铁心/铁心柱) 出库");
        List<WmsOutReceiptMaterialVo> wmsOutReceiptMaterialVos = (List<WmsOutReceiptMaterialVo>) map.get("records");

        List<ProductReceptionRequestDTO> list = new ArrayList<>();
        wmsOutReceiptMaterialVos.forEach(t -> {
            ProductReceptionRequestDTO requestDTO = new ProductReceptionRequestDTO();

            SapParamUtil.validWmsOutReceiptMaterial(t);
            
            WmsOutReceiptTaskMaterial one = wmsOutReceiptTaskMaterialService.getOne(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>().eq(WmsOutReceiptTaskMaterial::getOriginalSystemNumber, t.getOriginalSystemNumber())
                    .eq(WmsOutReceiptTaskMaterial::getOriginalSystemLineNumber, t.getOriginalSystemLineNumber()));

            SapParamUtil.validOutTaskRecord(one);

            ProductShipmentsRequestDTO productShipmentsRequestDTO = JSONObject.parseObject(one.getExtraText(), ProductShipmentsRequestDTO.class);
            requestDTO.setUbDumpNumber(t.getOriginalSystemNumber());
            requestDTO.setUbLineProject(t.getOriginalSystemLineNumber());
            requestDTO.setFreightVoucher(productShipmentsRequestDTO.getFreightVoucher());
            requestDTO.setFreightVoucherProject(productShipmentsRequestDTO.getFreightVoucherProject());
            requestDTO.setSaleOrderCode(productShipmentsRequestDTO.getSaleOrderCode());
            requestDTO.setSaleOrderCodeLine(productShipmentsRequestDTO.getSaleOrderCodeLine());
            String date = DateUtil.formatDate(productShipmentsRequestDTO.getDeliveryDate()).replace("-", "");
            requestDTO.setDeliveryDate(date);
            requestDTO.setUuid(productShipmentsRequestDTO.getUuid());
            list.add(requestDTO);
        });
        List<ProductReceptionResponseDTO> productReceptionResponseDTOS = sapDockingRepeaterService.repeaterSapProductHarvest(list);
        return productReceptionResponseDTOS;
    }
}
